package cn.hotel.controller.user;

import cn.hotel.entity.User;
import cn.hotel.response.ResponseTool;
import cn.hotel.response.Result;
import cn.hotel.service.UserService;
import cn.hotel.utils.EMS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Resource
    private UserService userService;

    private static HttpSession staticSession;

    /**
     *普通用户注册
     * @param user
     * @return  1表示注册成功，0表示注册失败
     */
    @PostMapping("/register")
    public Result Register(User user){
        log.info("user信息{}",user);

        int count = userService.saveUser(user);

        if (count==1){
            return ResponseTool.success("1");
        }
        return ResponseTool.failed("0");
    }

    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        request.getSession().invalidate();
        return ResponseTool.success("退出成功");
    }

    /**
     * 根据用户id查询对应的用户
     * @param id
     * @return
     */
    @GetMapping("/getUserById/{id}")
    public Result getUserById(@PathVariable("id") String id){
        log.info("id{}",id);
        if (!StringUtils.hasText(id)){
            return ResponseTool.failed("查询失败");
        }

        User user = userService.queryUserById(id);
        log.info("user=={}",user);
        if (user!=null){
            return ResponseTool.success(user);
        }

        return ResponseTool.failed("查询失败");
    }

    @PostMapping("/getCode")
    public Result getCode(String phone,HttpSession session){
        log.info("phone==={}",phone);
        if (!StringUtils.hasText(phone)){
            return ResponseTool.failed("请输入正确手机号");
        }
        Random rand = new Random();
        int code = rand.nextInt(9000) + 1000;
        log.info("code=={}",code);
        staticSession = session;
        staticSession.setAttribute("code",code);
        log.info("session.getAttribute(code)=={}",session.getAttribute("code"));
        if (userService.getCode(phone,code)==1){
            return ResponseTool.success();
        }
        return ResponseTool.failed("获取验证码失败");
    }

    @PostMapping("/retrievePassword")
    public Result retrievePassword(String phone,String password,String confirmPassword,String code){

        log.info("phone=={},password{},confirmPassword{},code{}",phone,password,confirmPassword,code);
        if (!( StringUtils.hasText(phone)&&StringUtils.hasText(password)&&StringUtils.hasText(confirmPassword)&&StringUtils.hasText(code) )){
            return ResponseTool.failed("请将信息填写完整");
        }

        if (!password.equals(confirmPassword)){
            return ResponseTool.failed("两次密码不一致");
        }

        String sessionCode = String.valueOf(staticSession.getAttribute("code"));
        System.out.println("====="+sessionCode);
        if (sessionCode.equals(code)){
            userService.changePassword(phone,password);
            return ResponseTool.success("修改成功请前往登录");
        }else {
            return ResponseTool.failed("验证码不对");
        }
    }
}
